﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Context
{
  public class ResourceContentElementReadWrite : XacmlElement
  {
    private XmlDocument _document = (XmlDocument) null;
    private string _contents;
    private XmlNameTable _nameTable;

    public virtual XmlDocument XmlDocument
    {
      get
      {
        if (this._document == null)
        {
          this._document = this._nameTable == null ? new XmlDocument() : new XmlDocument(this._nameTable);
          if (this._contents != string.Empty)
            this._document.LoadXml(this._contents);
        }
        return this._document;
      }
      set
      {
        this._document = value;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public ResourceContentElementReadWrite(XmlDocument document, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      this._document = document;
    }

    public ResourceContentElementReadWrite(XmlReader reader, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      if (reader.LocalName == "ResourceContent")
      {
        this._contents = reader.ReadInnerXml();
        this._nameTable = reader.NameTable;
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }
  }
}
